//
//  SelectLevelScene.m
//  z_boxem
//
//  Created by A B on 2/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SelectLevelScene.h"
#import "MainMenuScene.h"
@implementation SelectLevelScene

+(id) scene
{
    CCScene *scene = [CCScene node];
    
    SelectLevelScene *layer = [SelectLevelScene node];
    
    [scene addChild: layer];
    
    return scene;
}

-(id) init
{
    
    if( (self=[super init] )) {
        
        
        CCLabelTTF *title = [CCLabelTTF labelWithString:@"Select level" fontName:@"Courier" fontSize:64];
        title.position =  ccp(240, 280);
        [self addChild: title];
        
        CCLayer *menuLayer = [[CCLayer alloc] init];
        [self addChild:menuLayer];
        
        NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
        NSNumber *lvl = [currentDefaults objectForKey:@"Level"];
        if(lvl == Nil){
            lvl = [[NSNumber alloc] initWithInt:0];
        }
        
        CCMenu *menu = [CCMenu menuWithItems:nil];
        for(int i=1; i<=[lvl intValue]; i++){
            
            CCMenuItem *item = [CCMenuItemFont itemFromString:[[NSString alloc] initWithFormat: @"%d", i] target:self selector:@selector(play:)];  
            item.tag = i;
            [menu addChild:item];

        }
        
        CCMenuItem *backItem = [CCMenuItemFont itemFromString:@"Back" target:self selector:@selector(back:)];
        [menu addChild:backItem];
        
        [menu alignItemsVertically];
        [menuLayer addChild: menu];
        
        
    }
    return self;
}

-(void) play:(id) sender {
    CCMenuItemFont  *item = (CCMenuItemFont*) sender;
    [[CCDirector sharedDirector] replaceScene:[HelloWorldLayer sceneWith:item.tag]]; 
}
-(void) back:(id) sender {
    [[CCDirector sharedDirector] replaceScene:[MainMenuScene scene]]; 
}
- (void) dealloc
{
    
    [super dealloc];
}



@end
